from typing import Dict, Any
from multiprocessing import Process
from processing.calibration import loadCalibration
from utils.display import showImages, showUndistortImages, showDisparity
import numpy as np


class ShowImagesCommand:
    def __init__(self, shared_state: Dict[str, Any]):
        """初始化 ShowImagesCommand"""
        self._shared_state = shared_state

    def execute(self, args: Any = None) -> None:
        """执行显示图像命令"""
        # 检查相机是否已启动
        if not self._shared_state.get("cameras_started", False):
            print("相机未启动，请先启动相机")
            return

        left_queue = self._shared_state["left_camera_queue"]
        right_queue = self._shared_state["right_camera_queue"]

        process = Process(target=showImages, args=(left_queue, right_queue))
        process.start()
        process.join()


class ShowUndistImagesCommand:
    def __init__(self, shared_state: Dict[str, Any]):
        """初始化 ShowImagesCommand"""
        self._shared_state = shared_state

    def execute(self, args: Any = None) -> None:
        """执行显示图像命令"""
        # 检查相机是否已启动
        if not self._shared_state.get("cameras_started", False):
            print("相机未启动，请先启动相机")
            return

        # 从文件加载校准参数
        try:
            left_map1 = np.load("./config/left_map1.npy")
            left_map2 = np.load("./config/left_map2.npy")
            right_map1 = np.load("./config/right_map1.npy")
            right_map2 = np.load("./config/right_map2.npy")
        except Exception as e:
            print(f"无法加载校准数据: {e}")
            return

        left_queue = self._shared_state["left_camera_queue"]
        right_queue = self._shared_state["right_camera_queue"]

        process = Process(
            target=showUndistortImages,
            args=(
                left_map1,
                left_map2,
                right_map1,
                right_map2,
                left_queue,
                right_queue,
            ),
        )
        process.start()
        process.join()


class ShowDisparityCommand:
    def __init__(self, shared_state: Dict[str, Any]):
        """初始化 ShowDisparityCommand"""
        self._shared_state = shared_state

    def execute(self, args: Any = None) -> None:
        """执行显示视差图命令"""
        # 检查相机是否已启动
        if not self._shared_state.get("cameras_started", False):
            print("相机未启动，请先启动相机")
            return

        # 从文件加载校准参数
        try:
            stereo_calibration = loadCalibration("./config/stereo_calibration.yaml")
            left_map1 = np.load("./config/left_map1.npy")
            left_map2 = np.load("./config/left_map2.npy")
            right_map1 = np.load("./config/right_map1.npy")
            right_map2 = np.load("./config/right_map2.npy")
        except Exception as e:
            print(f"无法加载校准数据: {e}")
            return

        left_queue = self._shared_state["left_camera_queue"]
        right_queue = self._shared_state["right_camera_queue"]

        process = Process(
            target=showDisparity,
            args=(
                left_queue,
                right_queue,
                left_map1,
                left_map2,
                right_map1,
                right_map2,
            ),
        )
        process.start()
        process.join()
